﻿using SF.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.customer
{
    public partial class cur_alltop : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.pageSize = GetPageSize(6); //每页数量
            if (!Page.IsPostBack)
            {
                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            txtKeywords.Text = this.keywords;
            //商户 公众号 模版等信息
            Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            string busnieseNum = mgr.busnieseNum.ToString();
            string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
            string templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();

            BLL.customer bll = new BLL.customer(busnieseNum,appNum,templatesNum);
            List<Model.sf_user_info> lists = bll.getTopList();
            for (int i = 0; i < lists.Count; i++)
            {
                lists[i].isAttention = i + 1;
            }
            List<Model.sf_user_info> temp = new List<Model.sf_user_info>();
            


            int rowCount = 0;
            if (this.keywords.Length > 0)
            {
                var listWhere = lists.Where(n => n.userNum.IndexOf(this.keywords) > -1 || n.nickName.IndexOf(this.keywords) > -1 || n.province.IndexOf(this.keywords) > -1);
                lists = new List<Model.sf_user_info>();
                foreach (Model.sf_user_info u in listWhere) 
                {
                    lists.Add(u);
                }

                rowCount = listWhere.Count();
            }
            else 
            {
                this.rptList.DataSource = lists;
                rowCount = lists.Count;
            }


            for (int i = pageSize * (page - 1); i < lists.Count && i < pageSize * page; i++)
            {
                temp.Add(lists[i]);
            }
            this.rptList.DataSource = temp;
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("cur_alltop.aspx", "keywords={0}&page={1}", this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, rowCount, pageUrl, 8);
        }
        #endregion 


        #region 返回每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("cur_alltop_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_alltop.aspx", "keywords={0}", this.txtKeywords.Text));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("cur_alltop_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("cur_alltop.aspx", "keywords={0}", this.keywords));
        }

        /// <summary>
        /// 根据 团队人数和销售金额 对行加颜色
        /// </summary>
        /// <param name="users">团队人数</param>
        /// <param name="moneys">销售金额</param>
        /// <returns></returns>
        public string getColorByMoneys(object users, object moneys)
        {
            //获取配置 奖励参数
            IDictionary dict = ConfigurationManager.GetSection("userTopFun") as IDictionary;
            long one_users = Convert.ToInt64(dict["one_users"].ToString());
            long one_moenys = Convert.ToInt64(dict["one_moenys"].ToString());
            long two_users = Convert.ToInt64(dict["two_users"].ToString());
            long two_moenys = Convert.ToInt64(dict["two_moenys"].ToString());
            long three_moenys = Convert.ToInt64(dict["three_moenys"].ToString());
            long four_moenys = Convert.ToInt64(dict["four_moenys"].ToString());
            long five_moenys = Convert.ToInt64(dict["five_moenys"].ToString());
            long user = Convert.ToInt64(string.IsNullOrEmpty(users.ToString()) ? "0" : users);
            long money = (long)Convert.ToDecimal(string.IsNullOrEmpty(moneys.ToString()) ? "0" : moneys);
            //色彩 规则：5-紫，4-橙，3-黄，2-绿，1-红；
            string color = money >= five_moenys ? "purple" : money >= four_moenys ? "orange" : money >= three_moenys ? "yellow" : money >= two_moenys && user >= two_users ? "green" : money >= one_moenys && user >= one_users ? "red" : "white";
            return color;
        }
    }
}